package org.oneblog.controller;

import java.net.InetAddress;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.oneblog.controller.util.NetUtils;
import org.oneblog.data.util.JunitConsoleOut;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;

public class BaseController {

    public boolean isGood(HttpServletRequest request, String id) {

        try {
            InetAddress localAddress = NetUtils.getLocalAddress();
            String localIp = localAddress != null ? localAddress.getHostAddress() : "127.0.0.1";
            
            Cache cache = cacheManager.getCache("dayCache");
            
            String key = JunitConsoleOut.console("setGoot_{}_{}_{}", new Object[] {
                    id, NetUtils.getIpAddr(request), localIp
            });
            
            Integer c = cache.get(key, Integer.class);
            
            return c != null && c > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return false;
    }
    
    @Resource(name = "cacheManager")
    private EhCacheCacheManager cacheManager;
}
